﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using UA.Data.Model;

namespace UA.Framework.Authorization
{
    /// <summary>
    /// 表示用户登录验证的主体操作信息。
    /// </summary>
    public class UAPrincipal : IPrincipal
    {
        private UAIdentity identity;
        private int? _userID;
        private string _userCode;


        public UAPrincipal() { }

        public UAPrincipal(int userID)
        {
            this.SetIdentity(userID);
        }

        public UAPrincipal(string userCode)
        {
            this.SetIdentity(userCode);
        }



        public void SetIdentity(int userID)
        {
            this._userID = userID;
            this.identity = new UAIdentity(userID);
        }

        public void SetIdentity(string userCode)
        {
            this._userCode = userCode;
            this.identity = new UAIdentity(userCode);
        }



        public static UAPrincipal Current
        {
            get { return UAAuthentication.TokenSession; }
        }


        public IIdentity Identity
        {
            get { return this.UAIdentity; }
        }

        public UAIdentity UAIdentity
        {
            get { return this.identity; }
        }


        public void Refresh()
        {
            if (this.UAIdentity != null)
            {
                this.UAIdentity.Refresh();
            }
        }



        public bool HasUser
        {
            get { return this.UAIdentity != null && this.UAIdentity.HasUser; }
        }



        public User User
        {
            get { return this.UAIdentity == null ? null : this.UAIdentity.User; }
        }


        public Guid? UserKey
        {
            get { return this.UAIdentity == null ? null : this.UAIdentity.UserKey; }
        }

        public int? UserID
        {
            get { return this.UAIdentity == null ? 0 : this.UAIdentity.UserID; }
        }

        public string UserCode
        {
            get { return this.UAIdentity == null ? null : this.UAIdentity.UserCode; }
        }

        public string UserName
        {
            get { return this.UAIdentity == null ? null : this.UAIdentity.UserName; }
        }

        public string Password
        {
            get { return this.UAIdentity == null ? null : this.UAIdentity.Password; }
        }





        public bool IsInRole(string roleName)
        {
            if (this.identity == null)
            {
                return false;
            }
            return this.UAIdentity.IsInRole(roleName);
        }

        public bool IsInRoleByID(int roleID)
        {
            if (this.identity == null)
            {
                return false;
            }
            return this.UAIdentity.IsInRoleByID(roleID);
        }

        public bool IsInRoleByCode(string roleCode)
        {
            if (this.identity == null)
            {
                return false;
            }
            return this.UAIdentity.IsInRoleByCode(roleCode);
        }



    }
}
